<?php

namespace app\modules\Product\models;

use Yii;
use app\modules\Manage\models\User;
/**
 * This is the model class for table "invoice".
 *
 * @property int $id
 * @property int $product_id 团队ID
 * @property string $code invoice编码
 * @property int $currency 货币(美元)
 * @property string $price 金额
 * @property string $download_time 下载时间
 * @property int $status 1正常  2 禁用 
 * @property int $user_id 创建人ID
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class Invoice extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'invoice';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['product_id', 'currency','pay_type','status', 'user_id'], 'integer'],
            [['price'], 'number'],
            [['payment_date'], 'required'],
            [['download_time', 'created_at', 'updated_at','payment_date'], 'safe'],
            [['code'], 'string', 'max' => 32],
        ];
    }

     //保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){

            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;

            $count = Invoice::find()->count();

            $code = 100001+$count;

            $this->code = $code;
        }
        return parent::beforeSave($insert);

    }//}}}


    //获取
    public function getUserName(){
        $user = User::findOne($this->user_id);
        return $user ? $user->name : ' ';
    }


    //  获取团队信息
    public function getProduct(){
        return $this->hasOne(Product::className(), ['id' => 'product_id']);
    }

    //获取invoice详情
    public function getInvoiceDetail(){
        return $this->hasMany(InvoiceDetail::className(), ['invoice_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'product_id' => Yii::t('app.product', 'Product ID'),
            'code' => Yii::t('app.product', 'Invoice Code'),
            'currency' => Yii::t('app', 'Currency'),
            'price' => Yii::t('app.product', 'Invoice Price'),
            'download_time' => Yii::t('app.product', 'Download Time'),
            'payment_date' => Yii::t('app.product', 'Payment Date'),
            'pay_type' => Yii::t('app.product', 'Pay Type'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
